<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<html lang="zh">
    <%@include file="/lyear/common/head.jsp"%>
<body>

<div class="lyear-layout-web">
    <div class="lyear-layout-container">
        <!--左侧导航-->
        <%@include file="/lyear/common/aside.jsp" %>
        <!--End 左侧导航-->

        <!--头部信息-->
        <%@include file="/lyear/common/top.jsp" %>
        <!--End 头部信息-->

        <!--页面主要内容-->
        <main class="lyear-layout-content">

            <div class="container-fluid">

                <div class="row">
                    <div class="col-lg-12">
                        <div class="card">
                            <div class="card-toolbar clearfix">
                                <form class="pull-right search-bar" method="get" action="#!" role="form">
                                    <div class="input-group">
                                        <div class="input-group-btn">
                                            <input type="hidden" name="searchKey" id="search_key" value="nickname">
                                            <button class="btn btn-default dropdown-toggle" id="search-btn" data-toggle="dropdown" type="button" aria-haspopup="true" aria-expanded="false">
                                                昵称 <span h="nickname"></span>
                                            </button>
                                            <ul class="dropdown-menu">
                                                <li><a tabindex="-1" href="javascript:void(0)" data-field="nickname">昵称</a></li>
                                                <li><a tabindex="-1" href="javascript:void(0)" data-field="account">账号</a></li>
                                                <li><a tabindex="-1" href="javascript:void(0)" data-field="tel">电话</a></li>
                                            </ul>
                                        </div>
                                        <input type="text" class="form-control" id="search-value" value="${searchValue}" name="searchValue" placeholder="请输入" onkeydown="selectUserByCondition()">
                                    </div>
                                </form>
                                <div class="toolbar-btn-action">
                                    <a class="btn btn-primary m-r-5" href="#!"><i class="mdi mdi-plus"></i> 新增</a>
                                    <a class="btn btn-success m-r-5" href="#!" onclick="activeUsers()"><i class="mdi mdi-check"></i> 启用</a>
                                </div>
                            </div>
                            <div class="card-body">
                                <div class="alert alert-success" role="alert" style="display: none;" id="resSuccessDiv"></div>
                                <div class="alert alert-danger" role="alert" style="display: none;" id="resFailDiv"></div>

                                <div class="table-responsive">
                                    <table class="table table-bordered">
                                        <thead>
                                        <tr>
                                            <th>
                                                <label class="lyear-checkbox checkbox-primary">
                                                    <input type="checkbox" id="check-all"><span></span>
                                                </label>
                                            </th>
                                            <th>编号</th>
                                            <th>昵称</th>
                                            <th>头像</th>
                                            <th>邮箱</th>
                                            <th>微信</th>
                                            <th>状态</th>
                                            <th>操作</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                        <c:forEach items="${pageBean.result}" var="user">
                                            <tr>
                                                <td>
                                                    <label class="lyear-checkbox checkbox-primary">
                                                        <input type="checkbox" name="ids[]" value="${user.id}"><span></span>
                                                    </label>
                                                </td>
                                                <td>${user.id}</td>
                                                <td>${user.nickname}</td>
                                                <td>
                                                    <img src="${user.avatar}" height="100px" width="100px">
                                                </td>
                                                <td>${user.email}</td>
                                                <td>${user.wechat}</td>
                                                <td><font class="text-success">${user.status}</font></td>
                                                <td>
                                                    <div class="btn-group">
                                                        <a class="btn btn-xs btn-default" href="${pageContext.request.contextPath}/user/getInfoById/${user.id}" title="编辑" data-toggle="tooltip"><i class="mdi mdi-pencil"></i></a>
                                                        <a class="btn btn-xs btn-default" href="${pageContext.request.contextPath}/user/getById/${user.id}" title="查看" data-toggle="tooltip"><i class="mdi mdi-eye"></i></a>
                                                    </div>
                                                </td>
                                            </tr>
                                        </c:forEach>
                                        </tbody>
                                    </table>
                                </div>

                                <%-- 分页--%>
                                <%@include file="/lyear/common/page.jsp" %>
                                <!--End 分页-->

                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </main>
        <!--End 页面主要内容-->
    </div>
</div>

<script type="text/javascript" src="${pageContext.request.contextPath}/lyear/js/jquery.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/lyear/js/bootstrap.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/lyear/js/perfect-scrollbar.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/lyear/js/main.min.js"></script>
<!--对话框-->
<script src="${pageContext.request.contextPath}/lyear/js/jconfirm/jquery-confirm.min.js"></script>

<script type="text/javascript">
    function activeUsers(){
        let userId=[];
        $("input[name='ids[]']:checked").each(function(){
            userId.push($(this).val());

        })
        if (userId.length==0){
            $.confirm({
                title: '温馨提示',
                content: '请添加需要启用的用户',
                type: 'orange',
                typeAnimated: false,
                buttons: {
                    close: {
                        text: '关闭',
                    }
                }
            });
            return;
        }
       $.confirm({
            title: '启用用户',
            content: '确认启用吗？',
            buttons: {
                confirm: {
                    text: '确认',
                    action: function () {
                        $.ajax({
                            url: "${pageContext.request.contextPath}/user/activeById/" + userId,
                            type:"POST",
                            success: function (res) {
                                if (res.code == 200) {
                                    $("#resSuccessDiv").css("display", "block");
                                    $("#resSuccessDiv").text(res.data).delay(3000).hide(300);
                                    setTimeout("location.reload()",3000);
                                }
                                if (res.code == 500) {
                                    $("#resFailDiv").css("display", "block");
                                    $("#resFailDiv").text(res.data);
                                }
                            },
                            dataType: 'json'
                        })
                    }
                },
                cancel: {
                    text: '取消'
                }
            }
        });
    };

    function selectUserByCondition(e) {
        let evt = window.event || e;
        if (evt.keyCode == 13) { //回车事件
            let ss = $('#search-btn').html();
            let i1 = ss.indexOf("\"");
            let i2 = ss.lastIndexOf("\"");
            let searchKey = ss.substr(i1 + 1, i2 - i1 - 1);
            let searchValue = $("#search-value").val();

            $.get("/projshow/user/getUserByCondition/1/10");
        }
    }

    $(function () {
        $('.search-bar .dropdown-menu a').click(function () {
            let field = $(this).data('field') || '';
            $('#search_key').val(field);
            $('#search-btn').html($(this).text() + '<span h="' + field + '"></span>');
        });
    });

</script>
</body>
</html>